﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace OOLib
{
   /// <summary>
   /// A TextSection is a range of complete paragraphs within a text.
   /// </summary>
   /// <remarks>
   /// The content of the section may be the content of a link into another document, a link from the same document, 
   /// or the result of a DDE operation.
   /// TextSection instances can be linked from and to other texts.
   /// </remarks>
   public class TextSection : TextContent, INamed
   {
      #region Constructors

      /// <summary>
      /// Constructor
      /// </summary>
      /// <remarks>
      /// After using this constructor, the ComObj must be set manually!
      /// </remarks>
      public TextSection() :
         base()
      { }

      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="comObject">COM object</param>
      public TextSection(object comObject) :
         base(comObject)
      { }

      #endregion

      #region XTextSection
      
      /// <summary>
      /// Gets the parent section.
      /// </summary>
      /// <remarks>
      /// If the object is a child section, then this property returns the parent text section.
      /// !!! Not tested !!!
      /// </remarks>
      public TextSection ParentSection
      {
         get
         {
            CheckDisposed();
            return GetObjectWrapper<TextSection>(InvokeMethod("getParentSection"));
         }
      }

      /// <summary>
      /// Gets all text sections that are children of this text section (recursive). 
      /// </summary>
      /// <returns>Returns an array with all child sections.</returns>
      /// <remarks>!!! Not tested !!!</remarks>
      public TextSection[] GetChildSections()
      {
         CheckDisposed();
         var rawSections = (object[])InvokeMethod("getChildSections");
         var sections = new TextSection[rawSections.Length];
         for (int i = 0; i < rawSections.Length; ++i )
         {
            sections[i] = GetObjectWrapper<TextSection>(rawSections[i]);
         }
         return sections;
      }

      #endregion

      #region INamed

      /// <summary>
      /// Gets or sets the name of the object.
      /// </summary>
      /// <remarks>
      /// The name is generally unique in the container of the object.
      /// !!! Not tested !!!
      /// </remarks>
      public string Name
      {
         get 
         {
            CheckDisposed(); 
            return (string)InvokeMethod("getName");
         }
         set 
         {
            CheckDisposed(); 
            InvokeMethod("setName", value);
         }
      }

      #endregion

      #region TextSection properties
      
      /// <summary>
      /// Gets or sets a conditional expression.
      /// </summary>
      /// <remarks>
      /// If the result of the conditional expression is true and the property TextSection::IsVisible is false, then the section is hidden.
      /// !!! Not tested !!!
      /// </remarks>
      public string Condition
      {
         get 
         {
            CheckDisposed(); 
            return (string)GetProperty("Condition");
         }
         set 
         {
            CheckDisposed(); 
            SetProperty("Condition", value);
         }
      }

      /// <summary>
      /// Gets or sets if the section is visible.
      /// </summary>
      /// <remarks>
      /// If this property is false, the text section is hidden.
      /// !!! Not tested !!!
      /// </remarks>
      public bool IsVisible
      {
         get 
         {
            CheckDisposed(); 
            return (bool)GetProperty("IsVisible");
         }
         set 
         {
            CheckDisposed(); 
            SetProperty("IsVisible", value);
         }
      }

      /// <summary>
      /// Gets or sets if the section is protected.
      /// </summary>
      /// <remarks>
      /// If this property is true, the text section is protected and cannot be modified from the user interface.
      /// !!! Not tested !!!
      /// </remarks>
      public bool IsProtected
      {
         get 
         {
            CheckDisposed(); 
            return (bool)GetProperty("IsProtected");
         }
         set 
         {
            CheckDisposed(); 
            SetProperty("IsProtected", value);
         }
      }

      /// <summary>
      /// Gets or sets the FileLink for this section.
      /// </summary>
      /// <remarks>
      /// If this property is set, then the content of the section is read from the specified document.
      /// </remarks>
      public SectionFileLink FileLink
      {
         get 
         {
            CheckDisposed();
            return GetObjectWrapper<SectionFileLink>(GetProperty("FileLink"));
            
         }
      }

      /// <summary>
      /// Gets or sets the source of a file link in the document that is specified in TextSection::FileLink.
      /// </summary>
      /// <remarks>
      /// The source may be a text section or a bookmark. If TextSection::FileLink is empty, then the current document is 
      /// searched for the source. If this property is empty and TextSection::FileLink is set, then the complete document 
      /// content is linked into this section.
      /// </remarks>
      public string LinkRegion
      {
         get 
         {
            CheckDisposed(); 
            return (string)GetProperty("LinkRegion");
         }
         set 
         {
            CheckDisposed(); 
            SetProperty("LinkRegion", value);
         }
      }

      /// <summary>
      /// Gets or sets the type of the command string for a DDE operation. 
      /// </summary>
      /// <remarks>
      /// The type can be the name of the application that provides a DDE source.
      /// !!! Not tested !!!
      /// </remarks>
      public string DDECommandType
      {
         get 
         {
            CheckDisposed(); 
            return (string)GetProperty("DDECommandType");
         }
         set 
         {
            CheckDisposed(); 
            SetProperty("DDECommandType", value);
         }
      }

      /// <summary>
      /// Gets or sets the source file name of the command string for a DDE operation.
      /// </summary>
      /// <remarks>!!! Not tested !!!</remarks>
      public string DDECommandFile
      {
         get 
         {
            CheckDisposed(); 
            return (string)GetProperty("DDECommandFile");
         }
         set 
         {
            CheckDisposed(); 
            SetProperty("DDECommandFile", value);
         }
      }

      /// <summary>
      /// Gets or sets the source element of the command string for a DDE operation.
      /// </summary>
      /// <remarks>
      /// The element can be i.e. a name of a cell in a sheet or a bookmark.
      /// !!! Not tested !!!
      /// </remarks>
      public string DDECommandElement
      {
         get 
         {
            CheckDisposed(); 
            return (string)GetProperty("DDECommandElement");
         }
         set 
         {
            CheckDisposed(); 
            SetProperty("DDECommandElement", value);
         }
      }

      //todo implement ramaining properties

      #endregion
   }
}
